﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoSite.Data;
using InfoSite.Tools;
using Resources;

namespace WebClient
{
    public partial class News : System.Web.UI.Page
    {
        public int CurrentYear { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            Title = String.Format("{0} - {1}", SiteResources.resCompanyName, SiteResources.resNews);

            if (!Page.IsPostBack)
            {
                CurrentYear = FillYears();
                if (!Request.QueryString.HasKeys())
                {
                    FillMessages(CurrentYear);
                }
                else if (!String.IsNullOrEmpty(Request.QueryString["year"]))
                {
                    string sYear = Request.QueryString["year"];
                    int iYear;

                    if (int.TryParse(sYear, out iYear))
                    {
                        CurrentYear = iYear;
                        FillMessages(iYear);
                    }
                }
                else if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                {
                    string sSiteContentId = Request.QueryString["Id"];
                    int iSiteContentId;

                    if (int.TryParse(sSiteContentId, out iSiteContentId))
                    {
                        SiteContent sc = SiteContent.Get(iSiteContentId);
                        if (sc != null)
                        {
                            //mvCompanyMessages.ActiveViewIndex = 1;
                            //lContent.Text = sc.Content;
                            //lContentTitle.Text = sc.Title;
                            //lTitle.Text = sc.Title;
                            //lDate.Text = sc.CreateDate.ToString();
                            //lContentDescription.Text = sc.CreateDate.ToString();
                            //rptFiles.DataSource = sc.FileAttachements;
                            //rptFiles.DataBind();

                            Title = String.Format("{0} - {1}", SiteResources.resCompanyName, sc.Title);
                        }
                    }
                }
            }
        }

        private int FillYears()
        {
            IOrderedEnumerable<int> lYearList = SiteContent.ListAllContentYear((int)EnumHelper.ListContentType.News, true).OrderByDescending(i => i);
            //rptSubMenu.DataSource = lYearList;
            //rptSubMenu.DataBind();

            return lYearList.First();
        }

        private void FillMessages(int iYear)
        {
            lContentTitle.Text = String.Format("{0} ({1})", SiteResources.resNews, iYear);

            List<SiteContent> scList = SiteContent.ListAllContent((int)EnumHelper.ListContentType.News, true, iYear);
            rptList.DataSource = scList.OrderByDescending(t => t.CreateDate);
            rptList.DataBind();
        }
    }
}